<?php
// +----------------------------------------------------------------------
// | 有理想的地方，地狱都是天堂。
// +----------------------------------------------------------------------
// | Copyright @ df81.com 版权所有
// +----------------------------------------------------------------------
// | 作者: 南国老符 邮箱：1062129401@qq.com
// +----------------------------------------------------------------------

namespace app\admin\controller;
use app\admin\model\Keyword as keywordModel;
use app\admin\model\Module as moduleModel;
use app\admin\validate\Keyword as keywordValidate;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\View;

class Keyword extends Base{
    // 列表
    public function index(){
    	$keyword = input('keyword'); $order = ['sequence'=>'desc','id'=>'desc'];
		$rs = keywordModel::index($this->lang,$keyword,$order);
		
		View::assign('list',$rs);
		View::assign('total',$rs->total());
		View::assign('lastPage',$rs->lastPage());
		
        return view();
    }
	
    // 添加编辑
    public function edit($id=0){
        if(!request()->isPost()){
            $rs = Db::name('keyword')->where('id',$id)->find();
            View::assign('rs',$rs);
            
            // 模型
            $moduleList = moduleModel::index($this->lang, 1000);
            View::assign('moduleList',$moduleList);
            
			return view();
        }else{
			// 表单验证
			try {
	            validate(keywordValidate::class)->check(input('post.'));
	        } catch (ValidateException $e) {
	            return json(['code' => 1001, 'msg' => $e->getMessage(), 'data' => '']);
	        }
				
			try {
	            keywordModel::edit($id);
	            return json(['code' => 200, 'msg' => lang('c_success'), 'data' => array('id'=>$id)]);
	        } catch (Exception $e) {
	            return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => '']);
	        }
        }		
	}
	
	// 单条删除
    public function delete_one(){
		try {
			$id = input('id');
            $keyword = keywordModel::find($id);
        	$keyword->delete();
            return json(['code' => 200, 'msg' => lang('c_success'), 'data' => array('id'=>$id)]);
        } catch (Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => '']);
        }
    }	
	
	/* 批量删除，返回值：
    @ error:no_check 未勾选数据
    @ success 成功
    */
    public function delete_all(){
		$selectid = input('selectid/a');
		if(empty($selectid)){
			return json(['code' => 1001, 'msg' => lang('c_delete_check'), 'data' => array('selectid'=>$selectid)]);
		}
	    
		try {
			keywordModel::destroy($selectid);
            return json(['code' => 200, 'msg' => lang('c_success'), 'data' => array('selectid'=>$selectid)]);
        } catch (Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => '']);
        }
    }	
	
	// 批量更新
    public function update_all(){
		$id = input('id/a'); $sequence = input('sequence/a');
		foreach($id as $i => $rs){
            $data[] = ['id'=>$id[$i],'sequence'=>$sequence[$i]];
        }
        
		try {
			$keyword = new keywordModel;
			$keyword->saveAll($data,true);
            return json(['code' => 200, 'msg' => lang('c_success'), 'data' => array('id'=>$id)]);
        } catch (Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => '']);
        }
    }	
}